package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.PasswordEditFailedException;
import com.sky.exception.TYHException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启自动分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //开启查询数据
        Page<SetmealVO> page = setmealMapper.selectByPage(setmealPageQueryDTO);



        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getResult());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result insertByList(SetmealDTO setmealDTO) {
        //将数据添加道套餐表中
        Setmeal setmeal = Setmeal.builder().createTime(LocalDateTime.now()).updateTime(LocalDateTime.now()).
                createUser(BaseContext.getCurrentId())
                .updateUser(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insertByList(setmeal);
        //将菜品拿出来 添加到数据库


        Long id=setmealMapper.select(setmeal.getName());

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
            setmealDishMapper.insertByList(setmealDishes);
        }
        log.info("看这里");
        return Result.success();
    }

    @Override
    public void updateByStatus(Integer status, Integer id) {
        //根据id查询 并且修改状态
        setmealMapper.updateByStatus(status,id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //判断 查询的结果 是否有启用的
        for (Long id : ids) {
            //查询id 如果套餐是启用的则不能删除
            Setmeal setmeal=setmealMapper.selectByIds(id);
            if (setmeal.getStatus() ==1){
                throw new TYHException("此套餐正在出售 不可删除");
            }
            //如果这个id不是启用状态 则会直接删除
            setmealMapper.deleteById(setmeal.getId());
            //同时 也要删除 相关联的dish表
            setmealDishMapper.deleteById(setmeal.getId());
        }
    }

    @Override
    public SetmealVO selectByid(Long id) {
        SetmealVO setmealVO=setmealMapper.selectByid(id);
        return setmealVO;
    }

    @Override
    @Transactional
    public void putByList(SetmealDTO setmealDTO) {
        //我要先修改数据 修改后 将 菜品给删除 再进行添加
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //修改完后 去删除菜品信息

        setmealDishMapper.deleteByid(setmeal.getId());
        //重新添加口味信息
        setmealDishMapper.putByList(setmeal.getId());
    }
}




